package com.zzedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzSchoolArchives;
import com.zzedu.dto.ZzSchoolArchivesDTO;
import com.zzedu.framework.transaction.ITransactionHandlerService;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.service.IZzSchoolArchivesService;
import com.zzedu.service.IZzUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 学校资料Controller
 *
 * @author Mr.Wang
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/school/archives")
public class ZzSchoolArchivesController extends BaseController {
    @Autowired
    IZzSchoolArchivesService zzSchoolArchivesService;

    @Autowired
    IOssClientService ossClientService;

    @Autowired
    ITransactionHandlerService transactionHandlerService;

    @Autowired
    IZzUploadFileService zzUploadFileService;

    /**
     * 查询学校资料列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzSchoolArchivesDTO zzSchoolArchives) {
        IPage<ZzSchoolArchives> page = zzSchoolArchivesService.page(zzSchoolArchives.getPageDTO(), zzSchoolArchivesService.buildQuery(zzSchoolArchives));
        return getDataTable(page, zzSchoolArchivesService.convertVO(page.getRecords()));
    }

    /**
     * 导出学校资料列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:export')")
    @Log(title = "学校资料", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzSchoolArchivesDTO zzSchoolArchives) {
        List<ZzSchoolArchives> list = zzSchoolArchivesService.list(zzSchoolArchivesService.buildQuery(zzSchoolArchives));
        ExcelUtil<ZzSchoolArchives> util = new ExcelUtil<ZzSchoolArchives>(ZzSchoolArchives.class);
        util.exportExcel(ServletUtils.getResponse(), list, "学校资料数据");
    }

    @Log(title = "下载资料", businessType = BusinessType.EXPORT)
    @RequestMapping("/downArchives")
    public void downArchives(String fileName) throws IOException {
        byte[] data = ossClientService.download(fileName);
        try (OutputStream os = ServletUtils.getResponse().getOutputStream()) {
            os.write(data);
        }
    }


    /**
     * 新增保存学校资料
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:add')")
    @Log(title = "学校资料", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody ZzSchoolArchives zzSchoolArchives) {
        return toAjax(transactionHandlerService.runInTransaction(() -> {
            zzSchoolArchivesService.save(zzSchoolArchives);
            zzUploadFileService.updateRelation(String.valueOf(zzSchoolArchives.getId()), Lists.newArrayList(zzSchoolArchives.getFileId()));
            return true;
        }));
    }


    /**
     * 获取学校资料详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzSchoolArchivesService.getById(id));
    }


    /**
     * 修改保存学校资料
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:edit')")
    @Log(title = "学校资料", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody ZzSchoolArchives zzSchoolArchives) {
        return toAjax(zzSchoolArchivesService.updateById(zzSchoolArchives));
    }

    /**
     * 删除学校资料
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:archives:remove')")
    @Log(title = "学校资料", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzSchoolArchivesService.removeBatchByIds(ids));
    }
}
